/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.db.DbMailItem;
/*     */ import com.zimbra.cs.index.IndexDocument;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Note
/*     */   extends MailItem
/*     */ {
/*     */   private Rectangle mBounds;
/*     */   private static final String CN_BOUNDS = "bounds";
/*     */   
/*     */   public static class Rectangle
/*     */   {
/*     */     public int x;
/*     */     public int y;
/*     */     public int width;
/*     */     public int height;
/*     */     
/*     */     public Rectangle() {}
/*     */     
/*     */     public Rectangle(Rectangle r)
/*     */     {
/*  45 */       this.x = r.x;this.y = r.y;this.width = r.width;this.height = r.height; }
/*  46 */     public Rectangle(int ax, int ay, int awidth, int aheight) { this.x = ax;this.y = ay;this.width = awidth;this.height = aheight; }
/*     */     
/*  48 */     public Rectangle(String bounds) { if (bounds == null)
/*  49 */         return;
/*  50 */       String[] dimensions = bounds.split(",");
/*  51 */       if ((dimensions.length == 2) || (dimensions.length == 4)) {
/*  52 */         this.x = Integer.parseInt(dimensions[0]);this.y = Integer.parseInt(dimensions[1]);
/*  53 */         if (dimensions.length == 4) {
/*  54 */           this.width = Integer.parseInt(dimensions[2]);this.height = Integer.parseInt(dimensions[3]);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     public boolean equals(Rectangle r) {
/*  60 */       return (r != null) && (this.x == r.x) && (this.y == r.y) && (this.width == r.width) && (this.height == r.height);
/*     */     }
/*     */     
/*     */     public String toString() {
/*  64 */       return this.x + "," + this.y + "," + this.width + "," + this.height;
/*     */     }
/*     */   }
/*     */   
/*     */   public Note(Mailbox mbox, MailItem.UnderlyingData data)
/*     */     throws ServiceException
/*     */   {
/*  71 */     this(mbox, data, false);
/*     */   }
/*     */   
/*     */   public Note(Mailbox mbox, MailItem.UnderlyingData data, boolean skipCache) throws ServiceException {
/*  75 */     super(mbox, data, skipCache);
/*  76 */     if (this.mData.type != MailItem.Type.NOTE.toByte()) {
/*  77 */       throw new IllegalArgumentException();
/*     */     }
/*     */   }
/*     */   
/*     */   public String getSender()
/*     */   {
/*  83 */     return "";
/*     */   }
/*     */   
/*     */   public String getText()
/*     */   {
/*  88 */     return getSubject();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public Rectangle getBounds()
/*     */   {
/*  95 */     return new Rectangle(this.mBounds);
/*     */   }
/*     */   
/*     */   boolean isTaggable()
/*     */   {
/* 100 */     return true;
/*     */   }
/*     */   
/*     */   boolean isCopyable()
/*     */   {
/* 105 */     return true;
/*     */   }
/*     */   
/*     */   boolean isMovable()
/*     */   {
/* 110 */     return true;
/*     */   }
/*     */   
/*     */   boolean isMutable()
/*     */   {
/* 115 */     return true;
/*     */   }
/*     */   
/*     */   boolean canHaveChildren() {
/* 119 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static Note create(int id, Folder folder, String content, Rectangle location, Color color, MailItem.CustomMetadata custom)
/*     */     throws ServiceException
/*     */   {
/* 143 */     if ((folder == null) || (!folder.canContain(MailItem.Type.NOTE))) {
/* 144 */       throw MailServiceException.CANNOT_CONTAIN();
/*     */     }
/* 146 */     if (!folder.canAccess((short)4)) {
/* 147 */       throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the folder");
/*     */     }
/* 149 */     content = StringUtil.stripControlCharacters(content);
/* 150 */     if ((content == null) || (content.equals(""))) {
/* 151 */       throw ServiceException.INVALID_REQUEST("notes may not be empty", null);
/*     */     }
/* 153 */     if (location == null) {
/* 154 */       location = new Rectangle();
/*     */     }
/* 156 */     Mailbox mbox = folder.getMailbox();
/* 157 */     MailItem.UnderlyingData data = new MailItem.UnderlyingData();
/* 158 */     data.id = id;
/* 159 */     data.type = MailItem.Type.NOTE.toByte();
/* 160 */     data.folderId = folder.getId();
/* 161 */     if ((!folder.inSpam()) || (mbox.getAccount().getBooleanAttr("zimbraJunkMessagesIndexingEnabled", false))) {
/* 162 */       data.indexId = MailItem.IndexStatus.DEFERRED.id();
/*     */     }
/* 164 */     data.date = mbox.getOperationTimestamp();
/* 165 */     data.setSubject(content);
/* 166 */     data.metadata = encodeMetadata(color, 1, 1, custom, location);
/* 167 */     data.contentChanged(mbox);
/* 168 */     ZimbraLog.mailop.info("Adding Note: id=%d, folderId=%d, folderName=%s.", new Object[] { Integer.valueOf(data.id), Integer.valueOf(folder.getId()), folder.getName() });
/*     */     
/* 170 */     new DbMailItem(mbox).create(data);
/*     */     
/* 172 */     Note note = new Note(mbox, data);
/* 173 */     note.finishCreation(null);
/* 174 */     return note;
/*     */   }
/*     */   
/*     */   public List<IndexDocument> generateIndexData()
/*     */   {
/* 179 */     String toIndex = getText();
/* 180 */     IndexDocument doc = new IndexDocument();
/* 181 */     doc.addContent(toIndex);
/* 182 */     doc.addSubject(toIndex);
/* 183 */     doc.addPartName("NOTE");
/* 184 */     return Collections.singletonList(doc);
/*     */   }
/*     */   
/*     */   void setContent(String content) throws ServiceException {
/* 188 */     if (!isMutable()) {
/* 189 */       throw MailServiceException.IMMUTABLE_OBJECT(this.mId);
/*     */     }
/* 191 */     if (!canAccess((short)2)) {
/* 192 */       throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the note");
/*     */     }
/* 194 */     content = StringUtil.stripControlCharacters(content);
/* 195 */     if (Strings.isNullOrEmpty(content)) {
/* 196 */       throw ServiceException.INVALID_REQUEST("notes may not be empty", null);
/*     */     }
/* 198 */     if (content.equals(this.mData.getSubject())) {
/* 199 */       return;
/*     */     }
/* 201 */     addRevision(false);
/* 202 */     markItemModified(65568);
/*     */     
/* 204 */     this.mData.setSubject(content);
/* 205 */     this.mData.date = this.mMailbox.getOperationTimestamp();
/* 206 */     saveData(new DbMailItem(this.mMailbox));
/*     */   }
/*     */   
/*     */   protected void saveSubject() throws ServiceException {
/* 210 */     contentChanged();
/* 211 */     DbMailItem.saveSubject(this);
/*     */   }
/*     */   
/*     */   void reposition(Rectangle bounds) throws ServiceException {
/* 215 */     if (!isMutable()) {
/* 216 */       throw MailServiceException.IMMUTABLE_OBJECT(this.mId);
/*     */     }
/* 218 */     if (!canAccess((short)2)) {
/* 219 */       throw ServiceException.PERM_DENIED("you do not have sufficient permissions on the note");
/*     */     }
/* 221 */     if (bounds == null) {
/* 222 */       throw ServiceException.INVALID_REQUEST("must specify bounds", null);
/*     */     }
/* 224 */     if (bounds.equals(this.mBounds)) {
/* 225 */       return;
/*     */     }
/* 227 */     markItemModified(16384);
/* 228 */     this.mBounds = new Rectangle(bounds);
/* 229 */     saveMetadata();
/*     */   }
/*     */   
/*     */   void decodeMetadata(Metadata meta) throws ServiceException
/*     */   {
/* 234 */     super.decodeMetadata(meta);
/* 235 */     this.mBounds = new Rectangle(meta.get("l", null));
/*     */   }
/*     */   
/*     */   Metadata encodeMetadata(Metadata meta)
/*     */   {
/* 240 */     return encodeMetadata(meta, this.mRGBColor, this.mMetaVersion, this.mVersion, this.mExtendedData, this.mBounds);
/*     */   }
/*     */   
/*     */   private static String encodeMetadata(Color color, int metaVersion, int version, MailItem.CustomMetadata custom, Rectangle bounds) {
/* 244 */     MailItem.CustomMetadata.CustomMetadataList extended = custom == null ? null : custom.asList();
/* 245 */     return encodeMetadata(new Metadata(), color, metaVersion, version, extended, bounds).toString();
/*     */   }
/*     */   
/*     */   static Metadata encodeMetadata(Metadata meta, Color color, int metaVersion, int version, MailItem.CustomMetadata.CustomMetadataList extended, Rectangle bounds) {
/* 249 */     meta.put("l", bounds);
/* 250 */     return MailItem.encodeMetadata(meta, color, null, metaVersion, version, extended);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public String toString()
/*     */   {
/* 258 */     Objects.ToStringHelper helper = Objects.toStringHelper(this);
/* 259 */     appendCommonMembers(helper);
/* 260 */     helper.add("bounds", this.mBounds);
/* 261 */     return helper.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/Note.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */